using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FetCo.Data;
using ADESSA.Business;
using ADESSA.Entities;
using FetCo.Framework;
using FetCo.Web;
using System.IO;

public partial class MproductBuyadd : System.Web.UI.Page
{
    protected long Id
    {
        get
        {
            if (HttpContext.Current.Request.QueryString["Id"] == null)
                return long.MinValue;
            else
                return long.Parse(HttpContext.Current.Request.QueryString["Id"]);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadCurent();
        if (IsPostBack)
            return;
        LoadCurrunce();
        LoadUnitType();
        LoadCategoryPro();
        loadSubcat();
        loadSubSubcat();
        LoadDate();
        
        
    }
    #region LoadCurent
    private void LoadCurent()
    {
        ContentPlaceHolder c = Page.Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
        UserControl usc = c.FindControl("MainMenu1") as UserControl;
        HtmlAnchor hrfMproductBuyadd = (HtmlAnchor)usc.FindControl("hrfMproductBuyadd");
        hrfMproductBuyadd.Attributes.Add("class", "selected1");
    }

    #endregion
    #region Load Date
    private void LoadDate()
    {
        ADESSA.Entities.ProductBuy _ProductBuy = new ADESSA.Entities.ProductBuy();
        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
        {
            _ProductBuy = ProductBuyService.GetProductBuy(this.Id);
            if (_ProductBuy != null)
            {
                txtName.Value = _ProductBuy.Title;
                txtBriefDescription.Value = _ProductBuy.BriefDescription;
                if (!string.IsNullOrEmpty(_ProductBuy.Image))
                {
                    img.Src = "photos/" + _ProductBuy.Image;
                }
                else
                {
                    img.Visible = false;
                }

                ProductCat _ProductCatSubSub = ProductCatService.GetProductCat(_ProductBuy.CatId);
                if (_ProductCatSubSub != null)
                {
                    ProductCat _ProductCatSub = ProductCatService.GetProductCat(_ProductCatSubSub.Parrentid);
                    if (_ProductCatSub != null)
                    {
                        ddlProCat.SelectedValue = _ProductCatSub.Parrentid.ToString();
                        loadSubcat();
                        ddlSubCat.SelectedValue = _ProductCatSub.ProCateID.ToString();
                        loadSubSubcat();
                        ddlSubSubCat.SelectedValue = _ProductBuy.CatId.ToString();
                    }
                }
                txtproductkey.Value = _ProductBuy.KeyWord;
                if (_ProductBuy.ContactOption == 1)
                    rdocontacotion1.Checked = true;
                else if (_ProductBuy.ContactOption == 2)
                    rdocontacotion2.Checked = true;
                else if (_ProductBuy.ContactOption == 3)
                    rdocontacotion3.Checked = true;
                else
                {
                    rdocontacotion1.Checked = false;
                    rdocontacotion2.Checked = false;
                    rdocontacotion3.Checked = false;
                }
                txtExpireDate.Text = Utility.GetFormattedDate(_ProductBuy.ExpireDate);
                txtDetailedDescription.Value = _ProductBuy.DetailDescription;
                ddlCurency.SelectedValue = _ProductBuy.PriceRangeUnitId.ToString();
                txtLowest.Value = _ProductBuy.PriceRangeSmall;
                txtHighestt.Value = _ProductBuy.PriceRangeLarge;
                txtquantity.Value = _ProductBuy.QuantityRequire;
                ddlUnittype1.SelectedValue = _ProductBuy.QuantityRequireTypeId.ToString();
                txtRequiredCertification.Value = _ProductBuy.RequiredCertification;

            }
        }
        else
        {
            txtExpireDate.Text = Utility.GetFormattedDate(DateTime.Now);
        }
    }
    #endregion
    protected void ltbfinish_OnClick(object sender, EventArgs e)
    {
        ADESSA.Entities.ProductBuy _ProductBuy = new ADESSA.Entities.ProductBuy();
        if (this.Id != long.MinValue)
        {
            _ProductBuy = ProductBuyService.GetProductBuy(this.Id);
            _ProductBuy.UpdatedDate = DateTime.Now;
        }
        else
        {
            _ProductBuy.UpdatedDate = DateTime.Now;
            _ProductBuy.CreatedDate = DateTime.Now;
            if (Session["MemberId"] != null)
            {
                _ProductBuy.MemberId = long.Parse(Session["MemberId"].ToString());
            }
            else
            {
                Response.Redirect("MemberLogin.aspx");
            }
        }
        _ProductBuy.Enable = true;
        _ProductBuy.Title = txtName.Value;
        _ProductBuy.BriefDescription = txtBriefDescription.Value;
        #region Add Image
        if (FULImg.HasFile)
        {
            string path = Server.MapPath(RTISettings.NewsPictureFolder);
            try
            {
                if (_ProductBuy.Image != null && File.Exists(Path.Combine(path, _ProductBuy.Image)))
                {
                    File.Delete(Path.Combine(path, _ProductBuy.Image));
                }
                if (_ProductBuy.ThumbnailImage != null && File.Exists(Path.Combine(path, _ProductBuy.ThumbnailImage)))
                {
                    File.Delete(Path.Combine(path, _ProductBuy.ThumbnailImage));
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
            _ProductBuy.Image = Utilitys.UploadFileImage(FULImg.PostedFile, path, 100, 100, RTISettings.MaxFileSize, true);
            _ProductBuy.ThumbnailImage = Utilitys.UploadFileImage(FULImg.PostedFile, path, 500, 500, RTISettings.MaxFileSize, true);
        }
        #endregion
        _ProductBuy.CatId = long.Parse(ddlSubSubCat.SelectedValue);
        _ProductBuy.KeyWord = txtproductkey.Value;
        if (rdocontacotion1.Checked == true)
            _ProductBuy.ContactOption = 1;
        else if (rdocontacotion2.Checked == true)
            _ProductBuy.ContactOption = 2;
        else if (rdocontacotion3.Checked == true)
            _ProductBuy.ContactOption = 3;
        else
        {
            _ProductBuy.ContactOption = 0;
        }
        _ProductBuy.ExpireDate = Utility.ParseDateTime(txtExpireDate.Text);
        _ProductBuy.DetailDescription = txtDetailedDescription.Value;
        _ProductBuy.PriceRangeUnitId = long.Parse(ddlCurency.SelectedValue);
        if (txtLowest.Value != "0" || txtLowest.Value != "Lowest amount here")
            _ProductBuy.PriceRangeSmall = txtLowest.Value;
        if (txtHighestt.Value != "0" || txtHighestt.Value != "Highestt amount here")
            _ProductBuy.PriceRangeLarge = txtHighestt.Value;
        if (txtquantity.Value != "0" || txtquantity.Value != "Enter quantity you want to buy")
            _ProductBuy.QuantityRequire = txtquantity.Value;
        _ProductBuy.QuantityRequireTypeId = long.Parse(ddlUnittype1.SelectedValue);
        _ProductBuy.RequiredCertification = txtRequiredCertification.Value;
        _ProductBuy.Order = 100;
        if (this.Id != long.MinValue)
        {
            ProductBuyService.UpdateProductBuy(_ProductBuy);
        }
        else
        {
            ProductBuyService.CreateProductBuy(_ProductBuy);
        }
        Response.Redirect("MProductBuy.aspx"); 

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (this.Id != long.MinValue)
        {
            ProductBuy _ProductBuy = ProductBuyService.GetProductBuy(this.Id);
            string path = Server.MapPath(RTISettings.NewsPictureFolder);

            if (_ProductBuy.Image != null && File.Exists(Path.Combine(path, _ProductBuy.Image)))
            {
                File.Delete(Path.Combine(path, _ProductBuy.Image));
            }
            if (_ProductBuy.ThumbnailImage != null && File.Exists(Path.Combine(path, _ProductBuy.ThumbnailImage)))
            {
                File.Delete(Path.Combine(path, _ProductBuy.ThumbnailImage));
            }
            _ProductBuy.Image = "";
            _ProductBuy.ThumbnailImage = "";
            ProductBuyService.UpdateProductBuy(_ProductBuy);

        }
    }
    #region Load dropdownlist here
    private void LoadCurrunce()
    {
        ddlCurency.Items.Clear();
        ddlCurency.Items.Add(new ListItem("- - Curency - -", "0"));
        ADESSA.Entities.UnitPriceCollection _UnitPriceCollection = UnitPriceService.GetUnitPriceList(UnitPriceColumns.Order, OrderDirection.ASC);
        if (_UnitPriceCollection.Count > 0)
        {
            foreach (ADESSA.Entities.UnitPrice _UnitPrice in _UnitPriceCollection)
            {
                ddlCurency.Items.Add(new ListItem(_UnitPrice.Title, _UnitPrice.UnitPriceId.ToString()));
            }
        }
    }
    private void LoadUnitType()
    {
        ddlUnittype1.Items.Clear();
        ddlUnittype1.Items.Add(new ListItem("- - Unit type - -", "0"));
        ADESSA.Entities.UnitTypeCollection _UnitTypeCollection = UnitTypeService.GetUnitTypeList(UnitTypeColumns.Order, OrderDirection.ASC);
        if (_UnitTypeCollection.Count > 0)
        {
            foreach (ADESSA.Entities.UnitType _UnitType in _UnitTypeCollection)
            {
                ddlUnittype1.Items.Add(new ListItem(_UnitType.Name, _UnitType.UnitTypeId.ToString()));
            }
        }
    }

    private void LoadCategoryPro()
    {

        ddlProCat.Items.Clear();
        ddlProCat.Items.Add(new ListItem("- - - -Select Category- - - -", "0"));
        ddlSubCat.Items.Clear();
        ddlSubCat.Items.Add(new ListItem("- - - -Select SubCategory- - - -", "0"));
        ddlSubSubCat.Items.Clear();
        ddlSubSubCat.Items.Add(new ListItem("- - - -Select SubSubCategory- - - -", "0"));
        Criteria cri = new Criteria();
        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.CatLevel + "]", 0, Condition.EQUAL, Condition.AND);
        ProductCatCollection _ProductBuyCollection = ProductCatService.GetProductCatListByParam("[" + ProductCat._Table + "].[" + ProductCatColumns.SortOrder + "]", OrderDirection.ASC, cri.Criter);
        if (_ProductBuyCollection.Count > 0)
        {
            foreach (ProductCat _ProductBuy in _ProductBuyCollection)
            {
                ddlProCat.Items.Add(new ListItem(_ProductBuy.Name, _ProductBuy.ProCateID.ToString()));
            }
        }
    }
    private void loadSubcat()
    {
        ddlSubCat.Items.Clear();
        ddlSubCat.Items.Add(new ListItem("- - - -Select SubCategory- - - -", "0"));
        Criteria cri = new Criteria();

        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.CatLevel + "]", 1, Condition.EQUAL, Condition.AND);
        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.Parrentid + "]", Utility.ParseToBigint(ddlProCat.SelectedValue), Condition.EQUAL, Condition.AND);
        ProductCatCollection _ProductBuyCollection = ProductCatService.GetProductCatListByParam("[" + ProductCat._Table + "].[" + ProductCatColumns.SortOrder + "]", OrderDirection.ASC, cri.Criter);
        if (_ProductBuyCollection.Count > 0)
        {
            foreach (ProductCat _ProductBuy in _ProductBuyCollection)
            {
                ddlSubCat.Items.Add(new ListItem(_ProductBuy.Name, _ProductBuy.ProCateID.ToString()));
            }
        }
    }
    private void loadSubSubcat()
    {
        ddlSubSubCat.Items.Clear();
        ddlSubSubCat.Items.Add(new ListItem("- - - -Select SubCategory- - - -", "0"));
        Criteria cri = new Criteria();

        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.CatLevel + "]", 2, Condition.EQUAL, Condition.AND);
        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.Parrentid + "]", Utility.ParseToBigint(ddlSubCat.SelectedValue), Condition.EQUAL, Condition.AND);
        ProductCatCollection _ProductBuyCollection = ProductCatService.GetProductCatListByParam("[" + ProductCat._Table + "].[" + ProductCatColumns.SortOrder + "]", OrderDirection.ASC, cri.Criter);
        if (_ProductBuyCollection.Count > 0)
        {
            foreach (ProductCat _ProductBuy in _ProductBuyCollection)
            {
                ddlSubSubCat.Items.Add(new ListItem(_ProductBuy.Name, _ProductBuy.ProCateID.ToString()));
            }
        }
    }
    protected void ddlProSubCat_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        ddlSubCat.Items.Clear();
        ddlSubCat.Items.Add(new ListItem("- - - -Select SubCategory- - - -", "0"));
        Criteria cri = new Criteria();
        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.CatLevel + "]", 1, Condition.EQUAL, Condition.AND);
        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.Parrentid + "]", Utility.ParseToBigint(ddlProCat.SelectedValue), Condition.EQUAL, Condition.AND);
        ProductCatCollection _ProductBuyCollection = ProductCatService.GetProductCatListByParam("[" + ProductCat._Table + "].[" + ProductCatColumns.SortOrder + "]", OrderDirection.ASC, cri.Criter);
        if (_ProductBuyCollection.Count > 0)
        {
            foreach (ProductCat _ProductBuy in _ProductBuyCollection)
            {
                ddlSubCat.Items.Add(new ListItem(_ProductBuy.Name, _ProductBuy.ProCateID.ToString()));
            }
        }
    }
    protected void ddlSubCat_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        ddlSubSubCat.Items.Clear();
        ddlSubSubCat.Items.Add(new ListItem("- - - -Select SubSubCategory- - - -", "0"));
        Criteria cri = new Criteria();
        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.CatLevel + "]", 2, Condition.EQUAL, Condition.AND);
        cri.add("[" + ProductCat._Table + "].[" + ProductCatColumns.Parrentid + "]", Utility.ParseToBigint(ddlSubCat.SelectedValue), Condition.EQUAL, Condition.AND);
        ProductCatCollection _ProductBuyCollection = ProductCatService.GetProductCatListByParam("[" + ProductCat._Table + "].[" + ProductCatColumns.SortOrder + "]", OrderDirection.ASC, cri.Criter);
        if (_ProductBuyCollection.Count > 0)
        {
            foreach (ProductCat _ProductBuy in _ProductBuyCollection)
            {
                ddlSubSubCat.Items.Add(new ListItem(_ProductBuy.Name, _ProductBuy.ProCateID.ToString()));
            }
        }
    }
    #endregion Load about dropdownlist
}
